<?php 

/**
 * 个人中心管理控制器
 * 放一些公共的方法
 * @author 阿柱 <422852954@qq.com>
 */
class MemberController extends CommonController{
    /**
     *   默认访问方法
     */
    public function index(){
    	$this->commonSec();
		
//		我的提问
		$sql="SELECT * FROM hd_ask AS a JOIN hd_user AS u ON a.uid=u.uid JOIN hd_category AS c ON a.cid=c.cid WHERE u.uid={$_GET['uid']} ORDER BY RAND() LIMIT 5";
		$myaskData=M()->query($sql);
		$this->assign('myaskData',$myaskData);
		
//		我的回答
		$sql="SELECT * FROM hd_answer AS an JOIN hd_user AS u ON an.uid=u.uid JOIN hd_ask AS a ON an.asid=a.asid JOIN hd_category AS c ON c.cid=a.cid WHERE u.uid={$_GET['uid']} ORDER BY RAND() LIMIT 5";
		$myanswerData=M()->query($sql);
//		p($myanswerData);
		$this->assign('myanswerData',$myanswerData);		
		
        $this->display('member.html'); 
    }
	public function commonSec(){
//		判断session
		$this->judgeSession();
		$sql="SELECT * FROM hd_user WHERE uid={$_GET['uid']}";
		$login=M()->query($sql);
//			采纳率			
		if($login[0]['answer']!=0){
			$login[0]['c']=$login[0]['accept']/$login[0]['answer'] *100 ;
			$login[0]['c']=round($login[0]['c'],2).'%';
		}else{
			$login[0]['c']=0;
		}
		
//			等级
		$login[0]['lv']=$this->expToLv($login[0]['exp']);
		$this->assign('login',$login);
//		导航条内容处理Start***************
//		导航条头部的问题库
		$this->topCate();
//		导航条右侧的累计提问数
		$this->topCount();
//		导航条处理结束End*************
	}
	/**
	 * 我的提问
	 */
	public function myAsk(){
		$NY=isset($_GET['ny'])? (int)$_GET['ny']:0;
		$uid=(int)$_GET['uid'];
		$this->commonSec();
		if($NY==0){
//		未解决的问题
			$sql="SELECT * FROM hd_ask AS a JOIN hd_user AS u ON a.uid=u.uid JOIN hd_category AS c ON a.cid=c.cid WHERE u.uid={$uid} AND a.solve=0";
			$noSolveData=M()->query($sql);
			$this->assign('noSolveData',$noSolveData);			
		}else{
//			已解决的问题
			$sql="SELECT * FROM hd_ask AS a JOIN hd_user AS u ON a.uid=u.uid JOIN hd_category AS c ON a.cid=c.cid WHERE u.uid={$uid} AND a.solve=1";
			$solveData=M()->query($sql);
			$this->assign('solveData',$solveData);			
		}
		$this->display('my_ask.html');
	}
	
	/**
	 * 我的回答
	 */
	public function myAnswer(){		
		$all=isset($_GET['all'])?(int)$_GET['all']:0;
		$uid=(int)$_GET['uid'];
		$this->commonSec();
//		全部回答的问题
		if($all==0){
			$sql="SELECT * FROM hd_answer as an JOIN hd_ask as a ON an.asid=a.asid JOIN hd_category as c ON a.cid=c.cid WHERE an.uid={$uid}";
			$allAnswer=M()->query($sql);
			$this->assign('allAnswer',$allAnswer);
//			p($allAnswer);
		}else{
//			被采纳回答的问题
			$sql="SELECT * FROM hd_answer as an JOIN hd_ask as a ON an.asid=a.asid JOIN hd_category as c ON a.cid=c.cid WHERE an.uid={$uid} AND accept=1";
			$acceptAnswer=M()->query($sql);
			$this->assign('acceptAnswer',$acceptAnswer);
		}		
		
		$this->display('my_answer.html');
	}
	/**
	 * 我的等级
	 */
	public function myLevel(){
		$this->commonSec();
//		调用用户数据
		$uid=$_GET['uid'];
		$sql="SELECT * FROM hd_user WHERE uid={$uid}";
		$user=M()->query($sql);
		$user[0]['lv']=$this->expToLv($user[0]['exp']);
		$levelUp=(int)$user[0]['lv']+1;
		$exp=$this->lvToExp($levelUp);
		if(is_string($exp)){
			$gapExp=$exp;
		}else{
			$gapExp=$exp-$user[0]['exp'];
		}
//		p($levelUp); 	  	
		$this->assign('gapExp',$gapExp);
		$this->display('my_level.html');
	}
	/**
	 * 我的金币
	 */
	public function myGold(){
		$this->commonSec();
		
		$this->display('my_gold.html');
	}
	/**
	 * 我的头像
	 */
	public function myFace(){
		$this->commonSec();
		if(IS_POST){
			$sql="SELECT * FROM  hd_user WHERE uid={$_SESSION['uid']}";
			$face=M()->query($sql);
//			如果已经上传头像的话就会把之前的头像覆盖掉，并且把头像文件删除
			if($face[0]['face']){
				$facePath=explode('/', $face[0]['face']);
//				移除该用户的头像图片
				unlink($face[0]['face']);
			}
			//执行上传 实例化Upload也可以不传参数
			$upload = new Upload(array('jpg','png','gif'),5,'./Public/Home/Face');
//			接收上传之后的完整路径
			$upinfo = $upload->up();
			$sql="UPDATE hd_user SET face='{$upinfo[0]}' WHERE uid={$_SESSION['uid']}";
			M()->exec($sql);
//			$this->error('上传成功');
			//如果上传失败
			if(!$upinfo){
				//上传错误
				$this->error($upload->error);
			}
		}
		$face=$this->judgeSession();
		$this->assign('face', $face);
		$this->display('my_face.html');
	}
 }
 

 
 
 
 
 
 










 ?>